package com.datastructures2.sort;

/**
 * @author MaoLin Wang
 * @date 2020/2/1823:18
 */
public class SelectSort {
    public static <T extends Comparable<? super T>> void selectSort(T[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            T min = arr[minIndex];
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j].compareTo(min) < 0) {
                    minIndex = j;
                    min = arr[j];
                }
            }
            if (minIndex != i) {
                arr[minIndex] = min;
                arr[i] = min;
            }

        }
    }

    public static <T extends Comparable<? super  T>> void  select(T[]arr){

        T min;
        int minIndex;
        for (int i = 0; i <arr.length ; i++) {
            min=arr[i];
            minIndex=i;

            for (int j=i+1;j<arr.length;j++){
                if (arr[j].compareTo(min)<0){
                    min=arr[j];
                    minIndex=j;
                }
            }
            if (minIndex!=i){
                arr[minIndex]=arr[i];
                arr[i]=min;
            }
        }

    }

    public static void main(String[] args) {
        Integer[] arr = {2, 44, 1, 2, 6, 4};
        select(arr);
        for (Integer i : arr) {
            System.out.println(i);
        }
    }
}
